#include "userlogin.h"
#include "ui_userlogin.h"
#include <QMessageBox>
#include "service.h"
UserLogin::UserLogin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserLogin)
{
    ui->setupUi(this);
    this->myDatabase.getConnection();
}
UserLogin::~UserLogin()
{
    delete ui;
}

void UserLogin::on_Register_clicked(){
    QString userInputAccount = ui->Edit_account->text();
    QString userInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(userInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (userInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_user WHERE uaccount = :userInputAccount");
        sql.bindValue(":userInputAccount", userInputAccount);
        sql.exec();

        if(sql.first()){
        //如果用户名重复，则提示用户名重复
            QMessageBox::warning(this,"ERROR","用户名重复");//如果用户名重复，则提示用户名重复
        }
        else{
        //如果用户名不重复，添加数据进入数据库
            sql.prepare("INSERT INTO tb_user(uaccount,upassword) values(:userInputAccount,:userInputPassword)");
            sql.bindValue(":userInputAccount", userInputAccount);
            sql.bindValue(":userInputPassword", userInputPassword);
            sql.exec();
            QMessageBox::information(this,"提示","注册成功！！",QMessageBox::Yes);
        }
    }
}

void UserLogin::on_Quit_clicked()
{
    this->~UserLogin();
}


void UserLogin::on_Login_clicked()
{
    QString userInputAccount = ui->Edit_account->text();
    QString userInputPassword = ui->Edit_password->text();
    //判断用户名和密码是否为空
    if(userInputAccount==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Account can't be NULL!");
    }
    else if (userInputPassword==""){
        ui->Warning->setStyleSheet("color:red;");
        ui->Warning->setText("Warning:Password can't be NULL!");
    }
    else
    {
        QSqlQuery sql;

        sql.prepare("SELECT * FROM tb_user WHERE uaccount = '" + userInputAccount+"'");
        sql.exec();

        bool nFlag = 0;
        QString userAccount;
        QString userPassword;
        while(sql.next()){
            userAccount = sql.value("uaccount").toString();
            userPassword = sql.value("upassword").toString();
            if(userInputAccount == userAccount && userInputPassword == userPassword){
                QSqlQuery sql2;
                sql2.prepare("SELECT* FROM tb_user_VIP WHERE uaccount = '" + userInputAccount+"'");
                sql2.exec();
                bool isVIP = false;
                if(sql2.first())
                    isVIP = true;
                int uid = sql.value("uid").toInt();
                UserView* pUserView = new UserView(nullptr,uid,isVIP);
                nFlag = 1;

                this->pMainWindow->close();
                pUserView->show();
                this->~UserLogin();
                break;
            }
        }
        if(!nFlag)
            QMessageBox::information(this,"提示","登陆失败，请重试！",QMessageBox::Yes);
    }
}

